package cate.game.role.world.invite.comm;

import cate.common.table.d.GDOperation;
import cate.game.GameBody;
import cate.game.res.MixResAdder;
import cate.game.role.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteField;
import easy.java.practice.errorcode.EcException;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

public abstract class InviteNumTask {

    @NoteField("今日完成数量")
    public int doneNum;

    @NoteField(value = "已领取的奖励的tid")
    public List<Integer> received;

    public void init() {
    }

    @JsonIgnore
    protected abstract int getOperationSub();

    /** 领取第index个奖励，需要邀请的数量是多少 */
    @JsonIgnore
    protected abstract int getRequiredNumForReward(GameBody game, int index);

    /** 第index的奖励是啥 */
    @JsonIgnore
    protected abstract String getRewardCnf(GameBody game, int tid);

    public void tryGetReward(Role role, int tid) {
        String rwdCnf = getRewardCnf(role.getGame(), tid);
        if(StringUtils.isBlank(rwdCnf)) {
            throw new EcException("没有配置对应奖励").s();
        }
        if(doneNum < getRequiredNumForReward(role.getGame(), tid)) {
            throw new EcException("尚未达成目标").s();
        }
        if(received == null) {
            received = new ArrayList<>();
        }
        if(received.contains(tid)) {
            throw new EcException("已领取过奖励，不能重复领取了。").s();
        }
        received.add(tid);
        new MixResAdder()
                .setRes(rwdCnf)
                .setOperation(GDOperation.INVITE)
                .setOperationSub(getOperationSub())
                .setDialogReward(true)
                .exe(role);
    }

    public void reset() {
        this.doneNum = 0;
        this.received = null;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("今日完成数量="+doneNum);
        if(received == null) {
            sb.append(",未领取任何奖励");
        } else {
            sb.append(",已领取"+StringUtils.join(received, ","));
        }
        return sb.toString();
    }
}
